package com.sheep.jucdemo.part9.part9_3;

import java.text.SimpleDateFormat;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Date 2022/5/22 21:42
 * @Created by sheep
 */
public class Test9_3_3 {

    public static void main(String[] args) {
        System.out.println("2秒后开始打印当前时间"+getNowTime(System.currentTimeMillis()));
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(100);
        executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("当前时间"+getNowTime(System.currentTimeMillis()));
                try {
                    //这里休眠是为了和后续的方法做比较
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },2,1,TimeUnit.SECONDS);
    }
    public static String getNowTime(Long time){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
    }
}
